<?php
class Product_model extends CI_Model
{
	
	public function getDetail($tite)
	{
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name ,  org.origin_name', false);
		$this->db->from('tbl_products p ');
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
		$this->db->join('tbl_origin org', 'p.origin_id = org.origin_id ', 'left');
		
		$this->db->where('p.url',$tite);
		$query = $this->db->get();
// 		echo $this->db->last_query();die();
		return $query->row();
	}
	
	public function getDetailProduct($id)
	{
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name ,  org.origin_name', false);
		$this->db->from('tbl_products p ');
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
		$this->db->join('tbl_origin org', 'p.origin_id = org.origin_id ', 'left');
	
		$this->db->where('p_id',$id);
		$query = $this->db->get();
		// 		echo $this->db->last_query();die();
		return $query->row();
	}


	
	
	function getAllSearch($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->like('title',$search_param);
	
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
		
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
		
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
		
		return array(
				'total_record' => $total_record,
				'tbl_category' => $query->result_array(),
				'sql' => $sql,
				
		);
	}
	
	
	function getAllSearchProduct($search_param, $start, $limit, $filter =  array()){

		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->like('title',$search_param);
		}
        if($filter != null) {
            if($filter['p'] != null and $filter['p'] != 0){
                $this->db->where('p.aprice_id', $filter['p']);
            }
            if($filter['h'] != null and $filter['h'] != 0){
                $this->db->where('p.origin_id', $filter['h']);
            }
            if($filter['l'] != null and $filter['l'] != 0){
                $this->db->where('p.brand_id', $filter['l']);
            }
            if($filter['c'] != null and $filter['c'] != 0){
                $this->db->where('p.capacity_id', $filter['c']);
            }
            if($filter['t'] != null and $filter['t'] != 0){
                $this->db->where('p.tech_id', $filter['t']);
            }
            if($filter['pr'] != null and $filter['pr'] != 0){
                $this->db->order_by("p.price", "desc");
            }
        }
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
		
		$this->db->order_by("p.price", "asc");
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();



		
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
		
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
				
		);
	}
	
	function getAllSearchProductt($search_param, $start, $limit){
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->like('title',$search_param);
	
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
	
		$this->db->order_by("p.price", "ASC");
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
        //echo $sql;
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
	
		);
	}


	function getAllSearchProductPrice($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->where('aprice_id',$search_param);
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
		
	
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
	
		);
	}
	
	
	function getAllSearchProductorigin($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->where('origin_id',$search_param);
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
	
	
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
	
		);
	}
	
	function getAllSearchProductbrand($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->where('brand_id',$search_param);
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
	
	
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
	
		);
	}
	
	function getAllSearchProductcapacity($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->where('capacity_id',$search_param);
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
	
	
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
	
		);
	}
    
    
    function getAllSearchProductfeatures($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name', false);
		$this->db->from('tbl_products p ');

		if($search_param){
			$this->db->where('tech_id',$search_param);
		}
		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
	
	
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,
	
		);
	}
	
	
	function getAllSearchCategory($search_param, $start, $limit, $filter = array()){

		$this->db->select('SQL_CALC_FOUND_ROWS   p . * , cat.cate_name,  cat.seotext', false);
		$this->db->from('tbl_products p ');
			
		if($search_param){
			$this->db->where('p.cate_id',$search_param);
		}

        if($filter != null) {

            if($filter['p'] != null and $filter['p'] != 0){
                $this->db->where('p.aprice_id', $filter['p']);
            }
            if($filter['h'] != null and $filter['h'] != 0){
                $this->db->where('p.origin_id', $filter['h']);
            }
            if($filter['l'] != null and $filter['l'] != 0){
                $this->db->where('p.brand_id', $filter['l']);
            }
            if($filter['c'] != null and $filter['c'] != 0){
                $this->db->where('p.capacity_id', $filter['c']);
            }
            if($filter['t'] != null and $filter['t'] != 0){
                $this->db->where('p.tech_id', $filter['t']);
            }
            if($filter['pr'] != null and $filter['pr'] != 0){
                $this->db->order_by("p.price", "desc");
            }
        }

		$this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');
	
		$this->db->order_by("p.price", "ASC");
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();

		//die($sql);
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		
	
		return array(
				'total_record' => $total_record,
				'tbl_products' => $query->result_array(),
				'sql' => $sql,

		);
	}

    function getAllSearchCategory2($search_param, $start, $limit){

        $this->db->select('SQL_CALC_FOUND_ROWS   p . *, p.url as purl ,cat.url, cat.cate_name,  cat.seotext', false);
        $this->db->from('tbl_products p ');

        if($search_param){
            $this->db->where('p.cate_id',$search_param);

        }
        $this->db->join('tbl_category cat', 'p.cate_id = cat.cate_id ', 'left');

        $this->db->order_by("p.price", "desc");
        $this->db->limit($limit, $start);

        $query = $this->db->get();
        $sql= $this->db->last_query();
        //die($sql);

        $this->db->select("FOUND_ROWS() as total_record");
        $rRes = $this->db->get();
        $total_records = $rRes->row_array();
        $total_record = $total_records['total_record'];


        return array(
            'total_record' => $total_record,
            'tbl_products' => $query->result_array(),
            'sql' => $sql,

        );
    }
	
	function getAllSearchDichvu($search_param, $start, $limit){
	
		$this->db->select('SQL_CALC_FOUND_ROWS   con . * ,', false);
		$this->db->from('tbl_content con ');
			
		if($search_param){
			$this->db->where('con.menu_id',$search_param);
	
		}
		
	
		$this->db->order_by("con.created", "desc");
		$this->db->limit($limit, $start);
	
		$query = $this->db->get();
		$sql= $this->db->last_query();
	
		$this->db->select("FOUND_ROWS() as total_record");
		$rRes = $this->db->get();
		$total_records = $rRes->row_array();
		$total_record = $total_records['total_record'];
		//echo $total_record;
	
		return array(
				'total_record' => $total_record,
				'news' => $query->result_array(),
				'sql' => $sql,
	
		);
	}
	
	
	public function getAllSlider()
	{
		$this->db->from('tbl_slider');
		$query = $this->db->get();
		return $query->row();
	}
	
	
}